package gccshop.servlet;

import gccshop.pojo.Seller;
import gccshop.service.impl.SellerServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public class SellerManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 调用业务层实现
	SellerServiceImpl ssi = new SellerServiceImpl();
	// 每页固定显示10个，以后如果要修改显示数，改10即可
	private int rowsPerPage = 10;

	// get请求方式
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	// post请求方式
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");// 动作"show"
		if (method.equals("show")) {
			doShow(request, response);
		}
		if (method.equals("add")) {
			doAdd(request, response);
		}
		if (method.equals("del")) {
			doDel(request, response);
		}
		if (method.equals("modify")) {
			doModify(request, response);
		}
		if (method.equals("update")) {
			doUpdate(request, response);
		}
	}

	public void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String sid= request.getParameter("sid");
		String shopName= request.getParameter("shopName");
		String shopLocation= request.getParameter("shopLocation");
		String golden= request.getParameter("golden");
		//填充需要修改的对象
		Seller s=new Seller();
		s.setId(Integer.parseInt(sid));
		s.setShopName(shopName);
		s.setShopLocation(shopLocation);
		s.setGolden(golden);
		//视修改情况包装回传数据
		JSONObject data = new JSONObject();
		if (ssi.modify(s)) {
			data.put("flag", "yes");
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.print(data.toString());
		pw.close();
	}
	
	
	//根据id找到需要修改的对象，输出到模态框
	public void doModify(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("ids");
		// System.out.println("sellerServlet:ID="+id);
		JSONObject data = new JSONObject();
		data.put("data", ssi.findEntity("Id", id));
		request.setAttribute("data", data);
		request.getRequestDispatcher("manage/seller/sellerEdit.jsp").forward(
				request, response);
	}

   //批量删除
	public void doDel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String ids[] = request.getParameter("ids_del").split(",");// 4,5 4 5
		JSONObject data = new JSONObject();
		if (ssi.delete(ids)) {
			data.put("flag", "yes");
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.print(data.toString());
		pw.close();
	}

	/**
	 * 此实现方法有一点点儿缺陷， 那就是下面的页链接单击后不能正确显示原来的内容 而是到了新内容，即为""的全搜索
	 * 要解决这个问题，可以先将搜索框中的内容保存在localStorage ,刷新后再取出回填，可参阅我的博客中商品类别列表数据的添加
	 */
	// 按关键字查询并显示数据到页面上
	public void doShow(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 查询出数据，并生成数据列表
		String pn = request.getParameter("pageNo");
		int pageNo = pn == null ? 1 : Integer.parseInt(pn);
		String fv = request.getParameter("findValue");
		String findValue = fv == null ? "" : fv;
		findValue = new String(findValue.getBytes("iso8859-1"), "utf-8");// 小心汉字问题
		ArrayList<Seller> dataList = ssi.findEntities(findValue, pageNo,
				rowsPerPage);
		// 计算出分页，并生成分页列表[1,2,3,4...12]
		ArrayList<Integer> pageList = new ArrayList<Integer>();
		for (int i = 1; i <= ssi.findTotal(findValue) / rowsPerPage + 1; i++) {
			pageList.add(i);
		}
		// 用json对象封装数据，类似将快递商品sp放入包裹bg
		JSONObject bg = new JSONObject();//
		// 将对应的商品放入包裹，如果没有商品，也要做一次放入操作，只是空包裹。
		if (dataList != null) {
			bg.put("pageList", pageList);// 分页列表，用于页导航
			bg.put("sellers", dataList);// 数据列表，用于表格显示数据
		} else {
			bg.put("pageList", 0);
			bg.put("sellers", null);
		}
		request.setAttribute("sf", bg);// 叫顺风揽件，将包裹递给顺风
		// 顺风发货，去单上正确的买家地址
		request.getRequestDispatcher("manage/seller/sellerManage.jsp").forward(
				request, response);
	}

	public void doAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String shopName = request.getParameter("shopName");
		String shopLocation = request.getParameter("shopLocation");
		String golden = request.getParameter("golden");

		Seller s = new Seller();
		s.setShopName(shopName);
		s.setShopLocation(shopLocation);
		s.setGolden(golden);

		JSONObject data = new JSONObject();
		if (ssi.add(s)) {
			data.put("flag", "yes");
		}
		// 设置输出端的内容类型及格式
		response.setContentType("text/html;charset=utf-8");
		// 取得输出流
		PrintWriter pw = response.getWriter();
		// 向输出流写入内容
		pw.print(data.toString());
		// 清空输出流，将内容推送到响应端
		pw.close();
	}

}